// IO Monad
import fs from "fs";
import fp from "lodash/fp.js";

class IO {
  static of(value) {
    return new IO(function () {
      return value;
    });
  }
  constructor(fn) {
    this._value = fn;
  }
  map(fn) {
    return new IO(fp.flowRight(fn, this._value));
  }

  join() {
    return this._value();
  }

  flatMap(fn) {
    return this.map(fn).join();
  }
}

function readFile(filename) {
  return new IO(function () {
    return fs.readFileSync(filename, "utf-8");
  });
}
function print(x) {
  return new IO(function () {
    console.log(x);
    return x;
  });
}

let r = readFile("package.json").map(fp.toUpper).flatMap(print);
console.log(r, r.join());
